// electron/main.js

// 控制应用生命周期和创建原生浏览器窗口的模组
const { app, BrowserWindow } = require('electron')
const SystemWindow = require('./system/window/SystemWindow');
const SystemTray = require('./system/tray/SystemTray');
const SystemIpc = require("./system/ipc/SystemIpc");


const NODE_ENV = process.env.NODE_ENV;
const path = require('path');

//主窗口
let mainWin = null;
//系统托盘
let systemTray = null;

app.whenReady().then(() => {
  //创建窗口
  mainWin = SystemWindow.createWindow();

  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) mainWin = SystemWindow.createWindow();
  })

  mainWin.loadURL(
    NODE_ENV === 'development'
      ? 'http://localhost:3000'
      : `file://${path.join(__dirname, '../dist/index.html')}`
  );

  // 打开开发工具
  if (NODE_ENV === "development") {
    mainWin.webContents.openDevTools()
  }

  //创建系统托盘 必须全局变量否则可能会产生被GC回收
  systemTray = SystemTray.createTray(app);
  //监听
  SystemIpc.ipcAll(mainWin);

})
